var orderIntTxnCd = $.trim($("#orderIntTxnCd").val());
$(function(){
	$(".tabsUl .tabsLabel").click(function(){
		var currentIdx = $(this).parent().find(".cur").index()-1,
			selectIdx = $(this).index()-1;
		if(selectIdx == currentIdx) {return false;}
		$(this).siblings("li").removeClass("cur");
		$(this).addClass("cur");
		
		var clickId = $(this).prop("id");
		showChannelById(clickId);
	});
	var payChannel = $("#payChannel").val();
	if($("#orderIntTxnCd").val()=="00200"){
		payChannel="";
	}
	showChannel(payChannel);
});

function autoSortTag(show){
	var num = [];
	$("#channelSelLi li").each(function(i,domele){
		if($(this).attr("index")!=-1){
			num.push($(this).attr("index"));
		}
	});
	num = num.sort(function(a,b){
		return a-b;
	});
	$.each(num,function(i,v){
		if(i>0){
			$("#channelSelLi li[index='"+v+"']").insertAfter($("#channelSelLi li[index='"+num[i-1]+"']"));
		}
	});
	if(show){
		var channel = $("#channelSelLi li[index='"+num[0]+"']");
		channel.addClass("cur");
		showChannelById(channel.prop("id"));
	}
}

var loginPwdCtrl;
var gopayPwdCtrl;

function showTabHide(t1,t2,t3,t4){
	if(t1==1){
		$("#bankPayTab").show();
		onLineBankShow();
	}else{
		$("#bankPayTab").hide();
	}
	if(t2==1){
		$("#gopayTab").show();
		if($.trim($("#loginEmail").val())==""){
			if(!loginPwdCtrl){
				loginPwdCtrl=new PwdControl("password","loginPwdDiv",222,28);
			}
		}else{
			if(!gopayPwdCtrl && !(orderIntTxnCd == '01300' || orderIntTxnCd == '01302')){
				gopayPwdCtrl=new PwdControl("payPassword","userPayPwdDiv",222,28);
			}
		}
	}else{
		$("#gopayTab").hide();
	}
	if(t3==1){
		$("#debitExpressPayTab").show();
	}else{
		$("#debitExpressPayTab").hide();
	}
	if(t4==1){
		$("#creditExpressPayTab").show();
	}else{
		$("#creditExpressPayTab").hide();
	}
}
function showChannel(channel){
	var showTag = false;
	if(channel=="01"){
		showTabHide(1,0,0,0);
	}else if(channel=="06"){
		showTabHide(0,1,0,0);
	}else if(channel=="08"){
		showTabHide(0,0,1,0);
	}else if(channel=="07"){
		showTabHide(0,0,0,1);
	}else{
		showTag = true;
	}
	autoSortTag(showTag);
}

function showChannelById(clickId){
	if(clickId == "gopayChannelSel"){
		showTabHide(0,1,0,0);
	}else if(clickId == "debitExpressChannelSel"){
		showTabHide(0,0,1,0);
	}else if(clickId == "creditExpressChannelSel"){
		showTabHide(0,0,0,1);
	}else if(clickId == "bankChannelSel"){
		showTabHide(1,0,0,0);
	}
}
